package com.jsls.netspri.controller.data;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jsls.netspri.dao.SingleDataDao;
import com.jsls.netspri.entity.Page;
import com.jsls.netspri.entity.SingleData;

@Transactional
@Controller
@RequestMapping("/data")
public class DataController {

	@Resource
	private SingleDataDao singleDataDao;

	@Resource
	private Page page;
	
	//分页显示检测数据列表
	@RequestMapping(value = "/list/{id}/{currentPage}", method = RequestMethod.GET)
	public String getSingleDataByUserId(
			@PathVariable(value = "id") int user_id,
			@PathVariable(value = "currentPage") int currentPage, Model model) {

		/*List<SingleData> datalist = singleDataDao.findByUserId(user_id);*/
		
		int totalRows = singleDataDao.findRowsByUserId(user_id);
		int pageSize = page.getPageSize();
		
		if(totalRows%pageSize==0){
			page.setTotalPage(totalRows/pageSize);
		}else{
			page.setTotalPage(totalRows/pageSize+1);
		}
		
		page.setUser_id(user_id);
		page.setCurrentPage(currentPage);
		
		List<SingleData> datalist = singleDataDao.findByUserId_page(page);
		
		model.addAttribute("detectiondata", datalist);
		model.addAttribute("page", page);
		return "data/datalist";
	}

	//删除检测数据
	@ResponseBody
	@RequestMapping(value = "/singlech/{single_data_id}", method = RequestMethod.DELETE)
	public boolean deleteSingleData(
			@PathVariable(value = "single_data_id") int single_data_id) {

		singleDataDao.deleteBySingleDataId(single_data_id);
		return true;
	}

	//图表显示检测数据
	@RequestMapping(value="/singlech/{single_data_id}",method=RequestMethod.GET)
	public String showData(@PathVariable(value = "single_data_id") int single_data_id,Model model){
		
		SingleData sd =singleDataDao.findBySingleDataId(single_data_id);
		//JSONArray jsonArray = JSONArray.fromObject(data);
		
		model.addAttribute("datalist", sd);
		//System.out.println(jsonArray);
		
		return "data/showdata";
	}
	
	
	//文本域显示检测数据具体数值信息
    @RequestMapping(value = "/singlech/download/{id}",method=RequestMethod.GET)
    public String fileDownload(@PathVariable(value="id")int id,Model model ) {
       
		SingleData sd =singleDataDao.findBySingleDataId(id);
		
		model.addAttribute("datalist", sd);
		
		return "data/download";
    }
	
	
}
